package cn.jekun.web.agent.service;

import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.jekun.component.base.bean.Page;
import cn.jekun.component.base.utils.SqlUtis;
import cn.jekun.component.base.validator.annotation.NotNull;
import cn.jekun.component.core.bean.gen.RsAgent;
import cn.jekun.component.core.bean.gen.RsUser;
import cn.jekun.component.core.bean.gen.TsAccount;
import cn.jekun.component.core.service.ComponentCoreService;
import cn.jekun.component.core.service.gen.RsUserAgentService;
import cn.jekun.component.core.service.gen.RsUserService;
import cn.jekun.component.core.service.gen.TsAccountService;
import cn.jekun.component.core.bean.gen.RsUserAgent;
import cn.jekun.web.agent.bean.UserAgent;

@Service(UserAgentService.BEAN_NAME)
public class UserAgentService extends ComponentCoreService {
	public static final String BEAN_NAME = "cn.jekun.web.agent.service.UserAgentService";
	public static final String NAMESPACE = "ACTION_USER_AGENT";

	@Override
	public String getNamespace() {
		return NAMESPACE;
	}

	@Resource(name = RsUserService.BEAN_NAME)
	RsUserService userService;

	@Resource(name = TsAccountService.BEAN_NAME)
	TsAccountService tsAccountService;
	
	@Resource(name=AccountService.BEAN_NAME)
	AccountService accountService;
	

	@Resource(name = RsUserAgentService.BEAN_NAME)
	RsUserAgentService rsUserAgentService;

	/**
	 * 用戶是否存在
	 * 
	 * @param userId
	 * @return
	 */
	public boolean exists(@RsUser.AnnoUserId String userId) {
		Map formMap = new HashMap();
		formMap.put(RsUser.COL_USER_ID, userId);
		SqlUtis.prepareSql(formMap, SqlUtis.getSQLRequired(formMap, "a." + RsUser.COL_USER_ID, Types.VARCHAR, SqlUtis.EQ));
		return this.userService.exists(userId);
	}

	/**
	 * 根据用户Id查找User
	 */
	public RsUser getRsUserByUserId(@RsUser.AnnoUserId String userId) {
		Map formMap = new HashMap();
		formMap.put(RsUser.COL_USER_ID, userId);
		SqlUtis.prepareSql(formMap, SqlUtis.getSQLRequired(formMap, "a." + RsUser.COL_USER_ID, Types.VARCHAR, SqlUtis.EQ));
		return this.userService.one(formMap);
	}

	/**
	 * 修改用户登录密码
	 */
	@Transactional
	public void updateUserLogPwd(RsUser user,@RsUser.AnnoUserId String userId) {
		Map formMap = new HashMap();
		formMap.put("rsUser", user);
		formMap.put(RsUser.COL_USER_ID, userId);
		SqlUtis.prepareSql(formMap, SqlUtis.getSQLRequired(formMap, "a."
				+ RsUser.COL_USER_ID, Types.VARCHAR, SqlUtis.EQ));
		userService.update(formMap); // 修改用户登录
	}
    
	/**
	 * 查找账户
	 * @param agentId
	 * @param ownerTypeId
	 * @return
	 */
	public TsAccount getTsAccount(@TsAccount.AnnoOwnerId String agentId,@TsAccount.AnnoAccountTypeId String ownerTypeId) {
	    Map formMap = new HashMap();
		formMap.put(TsAccount.COL_OWNER_ID, agentId);
		formMap.put(TsAccount.COL_OWNER_TYPE_ID, ownerTypeId); //设置支付类型
		SqlUtis.prepareSql(formMap, SqlUtis.getSQL(formMap, "a."
				+ TsAccount.COL_OWNER_TYPE_ID, Types.VARCHAR, SqlUtis.EQ),
				SqlUtis.getSQL(formMap, "a." + TsAccount.COL_OWNER_ID, Types.VARCHAR, SqlUtis.EQ));
		return this.tsAccountService.one(formMap);
	}
	
	/**
	 * 修改账户的支付密码
	 */
	@Transactional
	public void updateTsAccountPayPwd(@NotNull(message = "账户对象不为空")TsAccount account,@TsAccount.AnnoOwnerId String agentId){
		Map formMap = new HashMap();
		formMap.put(TsAccount.COL_OWNER_ID, agentId);
		formMap.put(TsAccount.COL_OWNER_TYPE_ID, account.getOwnerTypeId()); //设置支付类型
		formMap.put("tsAccount", account);
		SqlUtis.prepareSql(formMap, SqlUtis.getSQL(formMap, "a."
				+ TsAccount.COL_OWNER_TYPE_ID, Types.VARCHAR, SqlUtis.EQ),
				SqlUtis.getSQL(formMap, "a." + TsAccount.COL_OWNER_ID, Types.VARCHAR, SqlUtis.EQ));
		this.tsAccountService.update(formMap);
	}
	/**
	 * 查询代理商所有的操作员
	 * @param agentId
	 * @param userAgentId
	 * @param realName
	 * @param page
	 * @return
	 */
	public List  getUserAgentList(@RsAgent.AnnoAgentId String agentId,@RsUserAgent.AnnoUserAgentId String userAgentId,String realName,Page page) {
		Map formMap =  new HashMap();
		formMap.put(RsAgent.COL_AGENT_ID, agentId);
		formMap.put(UserAgent.USER_AGENT_ID, userAgentId);
		formMap.put(UserAgent.REAL_NAME, realName);
		SqlUtis.prepareSql(formMap, SqlUtis.getSQL(formMap,
				"a." + RsAgent.COL_AGENT_ID, Types.VARCHAR, SqlUtis.EQ),
				SqlUtis.getSQL(formMap,"a."+UserAgent.USER_AGENT_ID,Types.VARCHAR,SqlUtis.LIKE),
				SqlUtis.getSQL(formMap,"b."+UserAgent.REAL_NAME,Types.VARCHAR,SqlUtis.LIKE));
		return this.list(formMap, page);
	}
	
	public TsAccount getTsAccountByAgentId(String statementName,@RsAgent.AnnoAgentId String agentId){
		Map formMap = new HashMap();
		formMap.put("agentId", agentId);
		return this.accountService.one(this.accountService.getStatementName(statementName),formMap);
	}
	
	
//	/**
//	 * 修改
//	 */
//	@Transactional
//	public void editMemo(String userAgentId,String memo){
//		RsUserAgent rsUserAgent = new RsUserAgent();
//		rsUserAgent.setUserAgentId(userAgentId);
//		rsUserAgent.setMemo(memo);
//		
//		this.rsUserAgentService.update(rsUserAgent);
//	}
}
